package com.zheng.umbrella.web.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.umbrella.common.constant.UmbrellaConstant;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.UmbrellaRuleTimecard;
import com.zheng.umbrella.dao.model.UmbrellaRuleTimecardExample;
import com.zheng.umbrella.rpc.api.UmbrellaRuleTimecardService;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/16.
 */
@Controller
@Api(value = "时长卡管理", description = "时长卡管理")
@RequestMapping("/manage/timecard")
public class UmbrellaRuleTimecardController extends BaseController {


    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaRuleTimecardController.class);

    @Autowired
    private UmbrellaRuleTimecardService umbrellaRuleTimecardService;

    @Autowired
    private UpmsApiService upmsApiService;

    @ApiOperation(value = "时长卡首页")
    @RequiresPermissions("umbrella:timecard:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/timecard/index.jsp";
    }

    @ApiOperation(value = "时长卡列表")
    @RequiresPermissions("umbrella:timecard:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        UmbrellaRuleTimecardExample umbrellaRuleTimecardExample = new UmbrellaRuleTimecardExample();
        UmbrellaRuleTimecardExample.Criteria criteria = umbrellaRuleTimecardExample.createCriteria();
        criteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            umbrellaRuleTimecardExample.setOrderByClause(sort + " " + order);
        }
        if (StringUtils.isNotBlank(search)) {
//            UmbrellaRuleTimeCardExample.or()
            criteria.andNameLike("%" + search + "%");
        }
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(getLoginUserName());
        if(agentId != null){
            criteria.andAgentIdEqualTo(agentId);
        }else{
            criteria.andAgentIdEqualTo(-1);
        }
        List<UmbrellaRuleTimecard> rows = umbrellaRuleTimecardService.selectByExampleForOffsetPage(umbrellaRuleTimecardExample, offset, limit);
        long total = umbrellaRuleTimecardService.countByExample(umbrellaRuleTimecardExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增时长卡")
    @RequiresPermissions("umbrella:timecard:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {

        List<UpmsDict> umpsDictList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_RULE_TIMECARD_TYPE);
        modelMap.put("umpsDictList", umpsDictList);
        return "/manage/timecard/create.jsp";
    }

    @ApiOperation(value = "新增时长卡")
    @RequiresPermissions("umbrella:timecard:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UmbrellaRuleTimecard umbrellaRuleTimecard) {
        long time = System.currentTimeMillis();
        UpmsUser user = upmsApiService.selectUpmsUserByUsername(getLoginUserName());
        umbrellaRuleTimecard.setCtime(time);
        umbrellaRuleTimecard.setCuser(user.getUsername());
        umbrellaRuleTimecard.setAgentId(user.getAgentId());
        int count = umbrellaRuleTimecardService.insertSelective(umbrellaRuleTimecard);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除时长卡")
    @RequiresPermissions("umbrella:timecard:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
//        int count = umbrellaRuleTimecardService.deleteByPrimaryKeys(ids);
        int count = umbrellaRuleTimecardService.deleteLogicByPrimaryKeys(ids,UmbrellaRuleTimecard.class,null);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改时长卡")
    @RequiresPermissions("umbrella:timecard:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        List<UpmsDict> umpsDictList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_RULE_TIMECARD_TYPE);
        modelMap.put("umpsDictList", umpsDictList);
        UmbrellaRuleTimecard umbrellaRuleTimecard = umbrellaRuleTimecardService.selectByPrimaryKey(id);
        modelMap.put("umbrellaRuleTimecard", umbrellaRuleTimecard);
        return "/manage/timecard/update.jsp";
    }

    @ApiOperation(value = "修改时长卡")
    @RequiresPermissions("umbrella:timecard:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, UmbrellaRuleTimecard umbrellaRuleTimecard) {
        umbrellaRuleTimecard.setId(id);
        int count = umbrellaRuleTimecardService.updateByPrimaryKeySelective(umbrellaRuleTimecard);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }


}
